package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.BuyOftenGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.PaySuccessfulBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.TagTextView;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：猜你喜欢
 */
public class GuessLikeAdapter extends RecyclerView.Adapter<GuessLikeAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<BuyOftenGoodsBeen.DataBean> mList;
    private Context mContext;
    private int proportion;

    public GuessLikeAdapter(Context context, List<BuyOftenGoodsBeen.DataBean> list) {
        this.mList = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_guess_like, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (proportion != 0) {
            proportion = holder.ivFineSelect.getWidth() / holder.ivFineSelect.getHeight();
        }
        //顶部左边圆角
        RoundedFormation transformation = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_LEFT);
        //顶部右边圆角
        RoundedFormation transformation1 = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_RIGHT);
        //组合各种Transformation,
        MultiTransformation<Bitmap> mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(), transformation, transformation1);
        RequestOptions options = RequestOptions.bitmapTransform(mation).override(200 * proportion, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFineSelect);
//  头部标签
        holder.tvContent.setSingleTagAndContent(mList.get(position).getGoods_ensures(), mList.get(position).getTitle());
//        holder.tvContent.setText(mList.get(position).getTitle());
        if (mList.get(position).getUnit().isEmpty()) {
            holder.tvPriceBox.setText("¥" + mList.get(position).getMarketprice());
        } else {
            holder.tvPriceBox.setText("¥" + mList.get(position).getMarketprice() + "/" + mList.get(position).getUnit());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_content)
        TagTextView tvContent;
        @BindView(R.id.iv_fine_select)
        ImageView ivFineSelect;
        @BindView(R.id.tv_price_box)
        TextView tvPriceBox;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
